package com.zpark.provider.controller;

import com.zpark.provider.entity.Books;
import com.zpark.provider.service.IBooksService;
import com.zpark.provider.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName: com.zpark.provider.controller
 * @ClassName: BooksController
 * @Description:
 * @author: RZS
 * @date: 2023/6/13  14:38
 */
@RestController
@RequestMapping("/books")
public class BooksController {

    @Autowired
    private IBooksService booksService;

    @Value("${spring.application.name}")
    private String serverName;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/find/{id}")
    public ResponseResult<Books>  findById(@PathVariable("id") Integer id){
        return new ResponseResult<>(2000, "成功", booksService.findById(id));
    }

    @GetMapping("/find_all")
    public ResponseResult<List<Books>>  findAll(){
        return new ResponseResult<>(2000, "成功", booksService.findAll());
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult<Integer>  removeById(@PathVariable("id") Integer id){
        return new ResponseResult<>(2000, "成功", booksService.removeBooks(id));
    }

    // 返回当前服务的名字+端口号
    @GetMapping("/test")
    public ResponseResult<String> test(){
        String str = "服务名字为" + serverName + ", 端口号为：" + serverPort;
        return new ResponseResult<>(200, "成功", str);
    }
}
